package com.Two;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName ShowMeBug
 * @date 2021/4/26 10:35
 * @Version 1.0
 * @Description //来未来第二题
 * @USER root
 */
public class ShowMeBug {
    static class Node {
        int id;
        int parentId;
        String name;

        public Node(int id, int parentId, String name) 				{
            this.id = id;
            this.parentId = parentId;
            this.name = name;
        }
    }

    public static void main(String[] args) {
        List<Node> nodeList = Arrays.asList(
                new Node(1, 0, "AA"),
                new Node(2, 1, "BB"),
                new Node(3, 1, "CC"),
                new Node(4, 3, "DD"),
                new Node(5, 3, "EE"),
                new Node(6, 2, "FF"),
                new Node(7, 2, "GG"),
                new Node(8, 4, "HH"),
                new Node(9, 5, "II"),
                new Node(10, 0, "JJ"),
                new Node(11, 10, "KK"),
                new Node(12, 10, "LL"),
                new Node(13, 12, "MM"),
                new Node(14, 13, "NN"),
                new Node(15, 14, "OO")
        );
        print(nodeList);
    }

    public static void print(List<Node> nodeList) {

        List<Node> headNode = getHead(nodeList);
        for (Node node : headNode){
            System.out.println(node.name);
            printLeaf(1, node.id, nodeList);
        }

//       int size = nodeList.size();
//        for (int i = 0; i < size; i++) {
//            if (nodeList.get(i).parentId == 0) {
//                System.out.println(nodeList.get(i).name);
//            }
//            if (nodeList.get(i).parentId != 0) {
//                int parentId = nodeList.get(i).parentId;
//                int count= 0;
//                count = foundCountParentNode(parentId,nodeList,count);
//                StringBuffer stringBuffer = new StringBuffer();
//                for (int j = 0; j < count; j++) {
//                    stringBuffer.append("  ");
//                }
//                stringBuffer.append(nodeList.get(i).name);
//                System.out.println(stringBuffer.toString());
//            }
//        }

    }
    private static void printLeaf(int deep, int id, List<Node> nodeList) {
        for (Node node : nodeList){
            if (node.parentId == id){
                for (int i = 0; i < deep; i++) {
                    System.out.print("    ");
                }
                System.out.println(node.name);
                printLeaf(deep+1, node.id, nodeList);
            }
        }
    }

    public static List<Node> getHead(List<Node> nodeList){
        List<Node> headNode = new ArrayList<>();
        for (Node node : nodeList){
            if (node.parentId == 0){
                headNode.add(node);
            }
        }
        return headNode;
    }

//    private static int foundCountParentNode(int parentId, List<Node> nodeList, int count) {
//        List<Node> parentNode = nodeList.stream().filter(k->k.id == parentId).collect(Collectors.toList());
//        if (parentNode.size() != 0 ) {
//            count++;
//            return foundCountParentNode(parentId,nodeList,count);
//        } else {
//            return count;
//        }
//
//    }


}
